package tmt.usercenter.web.controller.validtor;

import com.tmt.spring.validation.TmtCommonValidator;
import org.apache.catalina.startup.UserConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import tmt.usercenter.web.domain.UserInfoConfig;
import tmt.usercenter.web.service.TmtUserService;
import tmt.usercenter.web.service.UserInfoConfigService;
import tmt.usercenter.web.vo.user.RegisterVo;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

@Component
public class UserRegisterValidator extends TmtCommonValidator<RegisterVo> {

    private static final Logger logger = LoggerFactory.getLogger(UserRegisterValidator.class);

    @Autowired
    private TmtUserService userService;

    @Override
    public boolean supports(Class<?> clazz) {
        return RegisterVo.class.equals(clazz);
    }

    @Override
    public String selectMethodName(RegisterVo obj, Field field, String methodName) {
        String result = null;
        if (Objects.equals(field.getName(), "idCardNum")){
            switch (obj.getCardType()){
                case ID_CARD_HK:
                    result = "validateIdCard_HK";
                    break;
                case ID_CARD_AM:
                    result = "validateIdCard_AM";
                    break;
                case ID_CARD_TW:
                    result = "validateIdCard_TW";
                    break;
                default:
                    result = "validateIdCard2";
                    break;
            }
        }
        return result;
    }

    @Override
    public void validate(Object target, Errors errors) {
        super.validate(target, errors);
    }

    @Override
    public boolean isRepeated(Object obj, String fieldName, Object fieldValue, boolean repeatable) {
        if (repeatable)
            return false;

        boolean isRep = false;
        switch (fieldName){
            case "username":
                isRep = userService.existsByUsername((String)fieldValue);
                break;
        }
        return isRep;
    }
}